package team.hyznrj.studentsys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.hyznrj.studentsys.dao.repository.CollegeMajorRepository;
import team.hyznrj.studentsys.dto.ResultDto;
import team.hyznrj.studentsys.dto.RoleDto;
import team.hyznrj.studentsys.entity.CollegeMajor;
import team.hyznrj.studentsys.entity.account.AccountInfo;
import team.hyznrj.studentsys.entity.account.key.RolePowerTableKey;
import team.hyznrj.studentsys.enums.AccountEnum;
import team.hyznrj.studentsys.service.RoleService;
import team.hyznrj.studentsys.utils.RedisUtil;
import team.hyznrj.studentsys.utils.ResultUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ReMidDream
 * @date 2018-05-08 13:00
 **/
@Api(value = "RoleController", description = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private CollegeMajorRepository collegeMajorRepository;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value="查询学院所属权限下的所有角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录凭证 <br> 不填",paramType = "query"),
    })
    @GetMapping("/fAllRole")
    public ResultDto<?> findAllRole(@CookieValue(value = "token",required = false) String token){

        // 从redis 获取 帐号的 collegeMajorId
        AccountInfo account = redisUtil.getAccount(token);
        String collegeMajorId = account.getCollegeMajorId();

        List<CollegeMajor> collegeMajorList = new ArrayList<>();

        if (collegeMajorId.equals(String.valueOf(AccountEnum.SUPER_ADMIN.getCode()))) {
            collegeMajorList = collegeMajorRepository.findAll();
        }else {
            collegeMajorList = collegeMajorRepository.findByParentId(collegeMajorId);
        }

        List<String> roleLevelId = collegeMajorList.stream().map(CollegeMajor::getId).collect(Collectors.toList());
        roleLevelId.add(collegeMajorId);

        return ResultUtil.Success(roleService.findAllRoleDto(roleService.findRoleId(roleLevelId)));
    }

    @ApiOperation(value="添加/更新 角色以及操作权限关系", notes="*传入参数只需要带中文解释的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID",paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名字",paramType = "query"),
            @ApiImplicitParam(name = "roleLevelId", value = "角色权限",paramType = "query"),
            @ApiImplicitParam(name = "powerDtoList[0].powerId", value = "操作权限IDS <br>测试只能单个<br> JS传数组",paramType = "query" )
    })
    @PostMapping("/iuRole")
    public ResultDto<?> insertUpdateRole(RoleDto roleDto){
        roleService.insertUpdateRole(roleDto);
        return ResultUtil.Success();
    }


    @ApiOperation(value="添加 角色与操作权限关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rptRoleId", value = "角色ID",paramType = "query"),
            @ApiImplicitParam(name = "rptPowerId", value = "操作权限ID",paramType = "query")
    })
    @PostMapping("/iuRolePower")
    public ResultDto<?> insertRolePower(RolePowerTableKey rolePowerTableKey){
        roleService.insertRelationshipBetweenRoleAndPower(rolePowerTableKey);
        return ResultUtil.Success();
    }


    @ApiOperation(value="删除 角色与操作权限关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rptRoleId", value = "角色ID",paramType = "query"),
            @ApiImplicitParam(name = "rptPowerId", value = "操作权限ID",paramType = "query")
    })
    @PostMapping("/dRolePower")
    public ResultDto<?> deleteRolePower(RolePowerTableKey rolePowerTableKey){
        roleService.deleteRelationshipBetweenRoleAndPower(rolePowerTableKey);
        return ResultUtil.Success();
    }


    @ApiOperation(value="删除角色以及操作权限关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID",paramType = "query"),
    })
    @PostMapping("/dRole")
    public ResultDto<?> deleteRole(String roleId){
        roleService.deleteRole(roleId);
        return ResultUtil.Success();
    }



}
